package com.sg.service.base.framework.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.framework.req.QuerySystemDockingDeptSourceDataDetailReq;
import com.sg.dto.base.framework.req.QuerySystemDockingDeptSourceDataListReq;
import com.sg.entity.DepartSourceData;
import com.sg.service.base.framework.MDepartSourceDataService;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.dal.mapper.mbg.DepartSourceDataMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MDepartSourceDataServiceImpl
        extends ServiceImpl<DepartSourceDataMapper, DepartSourceData>
        implements MDepartSourceDataService {

    @Autowired
    DepartSourceDataMapper departSourceDataMapper;

    /**
     * 1-3-17新增系统对接部门源数据[7399]
     * gen by moon at 9/16/2023, 1:10:35 AM
     */
    @Trace(operationName = "1-3-17新增系统对接部门源数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addSystemDockingDeptSourceData(DepartSourceData departSourceData) {
        departSourceData.setDeptSourceDataId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        departSourceData.setSubjectLifeCycle("EDITING");
        departSourceData.setIsArchive("FALSE");
        departSourceData.setIsEditing("TRUE");
        departSourceData.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        departSourceData.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        departSourceData.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        departSourceData.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        departSourceData.setIsValid("FALSE");
        departSourceData.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        departSourceData.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(departSourceData)) {
            return departSourceData.getDeptSourceDataId();
        } else {
            return "-1";
        }

    }

    /**
     * 1-3-17查询系统对接部门源数据详情[7400]
     * gen by moon at 9/16/2023, 1:10:38 AM
     */
    @Trace(operationName = "1-3-17查询系统对接部门源数据详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public DepartSourceData querySystemDockingDeptSourceDataDetail(QuerySystemDockingDeptSourceDataDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<DepartSourceData> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<DepartSourceData> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                DepartSourceData retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 1-3-17批量增系统对接部门源数据[7401]
     * gen by moon at 9/16/2023, 1:10:39 AM
     */
    @Trace(operationName = "1-3-17批量增系统对接部门源数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchAddSystemDockingDeptSourceData(List<DepartSourceData> departSourceData) {

        if (departSourceData == null || departSourceData.size() == 0) {
            return false;
        }

        return super.saveBatch(departSourceData.stream().map(item -> {
            item.setDeptSourceDataId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
            item.setSubjectLifeCycle("EDITING");
            item.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
            item.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
            item.setIsValid("FALSE");
            item.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            item.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            return item;
        }).collect(Collectors.toList()));
    }

    /**
     * 1-3-17修改系统对接部门源数据[7403]
     * gen by moon at 9/16/2023, 1:10:40 AM
     */
    @Trace(operationName = "1-3-17修改系统对接部门源数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSystemDockingDeptSourceData(DepartSourceData departSourceData) {
        departSourceData.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (departSourceData != null && departSourceData.getSubjectLifeCycle() != null && departSourceData.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            departSourceData.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        departSourceData.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (departSourceData != null && departSourceData.getSubjectLifeCycle() != null && departSourceData.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            departSourceData.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        return super.updateById(departSourceData);
    }

    /**
     * 1-3-17查询系统对接部门源数据列表[7413]
     * gen by moon at 9/16/2023, 1:10:43 AM
     */
    @Trace(operationName = "1-3-17查询系统对接部门源数据列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<DepartSourceData> querySystemDockingDeptSourceDataList(QuerySystemDockingDeptSourceDataListReq reqDto) {

        try {
            QueryWrapper<DepartSourceData> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(DepartSourceData.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(DepartSourceData.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(DepartSourceData.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<DepartSourceData>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
